source(paste0("~/Desktop/Research/R/support_test/hawkes_utilities.R"))
data_dir <- "~/Desktop/Research/lobster/"
save_dir <- "~/Disk/Research/image_support_test/"


model_config_list <- c("state",                                 #1
                       "time_customizedtwo",                    #2
                       "state_time_customizedtwo",              #3
                       "hawkes",                                #4
                       "state_hawkes",                          #5
                       "hawkes_time_customizedtwo",             #6
                       "state_hawkes_time_customizedtwo",       #7
                       "hawkes_LASSO",                          #8
                       "state_hawkes_time_customizedtwo_LASSO", #9
                       "hawkes_LASSO_0005",                     #10
                       "state_hawkes_time_customizedtwo_LASSO_0005") #11   


args <- commandArgs(trailingOnly=TRUE)
if(length(args) == 0){
  args_size <- 2
  model_config <- model_config_list[1]
}else{
  args_size <- as.numeric(args[1])
  model_config <-model_config_list[as.numeric(args[2])]
}

# "nosize" if arg is a even number; withsize" if arg odd number
if(args_size %% 2 == 0){
  config_size <- "nosize"
}else{
  config_size <- "withsize"
}


data_processed_dir <- paste0(save_dir, "/data_processed_", config_size, "/")
all_bigdt_files <- list.files(data_processed_dir)
all_bigdt_files <- all_bigdt_files[grep("rds", all_bigdt_files)]
#all_bigdt_files <- all_bigdt_files[grep("2019-01-03|2019-01-04|2019-01-07", all_bigdt_files)]
#all_bigdt_files <- all_bigdt_files[grep("2019-01-10|2019-01-15|2019-01-29", all_bigdt_files)]
#all_bigdt_files <- all_bigdt_files[grep("2019-01-02|2017-01-04|2019-01-07|2019-01-08|2019-01-11|2019-01-15|2019-01-23|2019-01-29", all_bigdt_files)]
#all_bigdt_files <- all_bigdt_files[grep("2019-01-02|2019-01-03|2019-01-07|2019-01-23", all_bigdt_files)]
#all_bigdt_files <- all_bigdt_files[grep("support20delta0.5", all_bigdt_files)]
#all_bigdt_files <- all_bigdt_files[!grepl("support30delta0.5", all_bigdt_files)]
#all_bigdt_files <- all_bigdt_files[!grepl("support60delta0.5", all_bigdt_files)]


cores = 8
out_dir <- paste0(save_dir, "/data_calculated_", config_size, "/")

for (each_id in seq(1,20000,cores)){
  
  rds_to_compute = all_bigdt_files[each_id:(each_id+cores-1)]
  if(anyNA(rds_to_compute)){
    next
  }
  
  for (rds_file_name in rds_to_compute){
    if(file.exists(paste0(out_dir, "hawkes_kernel_single.",model_config, ".",rds_file_name))){
      print("file exists!")
      rds_to_compute = rds_to_compute[-which(rds_to_compute==rds_file_name)]
    }
  }
  
  if(length(rds_to_compute)==0){
    next
  }
  
  hawkes_kernel_list <- parallel::mclapply(rds_to_compute,function(rds_file_name){
    file_path <- paste0(data_processed_dir, rds_file_name)
    cat("input: ", file_path, "\n")
    cat("output: ", paste0(out_dir, "hawkes_kernel_single.",model_config, ".",rds_file_name), "\n")
    bigdt <- readRDS(file_path)
    hawkes_kernel <- get_hawkes_kernel_bigdt(bigdt, model_config)
    dir.create(out_dir, showWarnings = FALSE)
    saveRDS(hawkes_kernel, paste0(out_dir, "hawkes_kernel_single.",model_config, ".",rds_file_name))
  },mc.cores = cores)
  
}

warnings()
